package com.sf.manager.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.sf.manager.CollectInfoManager;
import com.sf.manager.ProMarketBaseManager;
import com.sf.mapper.CollectInfoMapper;
import com.sf.mapper.CollectMemberMapper;
import com.sf.mapper.RegInfoMapper;
import com.sf.model.CollectInfo;
import com.sf.model.CollectInfoExample;
import com.sf.model.CollectMember;
import com.sf.model.CollectMemberExample;
import com.sf.model.CollectStatus;
import com.sf.model.ProMarketBase;
import com.sf.model.RegInfo;
import com.sf.model.RegInfoExample;
import com.sf.util.DateUtil;
import com.sf.util.MarathonConst;

/**
 * 集货团信息Manager实现类
 *
 * @author 604625
 */
@Component
public class CollectInfoManagerImpl implements CollectInfoManager {

    @Autowired
    private CollectInfoMapper collectInfoMapper;
    @Autowired
    private ProMarketBaseManager proMarketBaseManager;
	@Autowired
	private CollectMemberMapper collectMemberMapper;
	
	@Autowired
	private RegInfoMapper refInfoMapper;
    /**
     * 按条件计数
     *
     * @param example 条件
     * @return int 记录总数
     */
    @Override
    public int countByExample(CollectInfoExample example) {
        return collectInfoMapper.countByExample(example);
    }

    /**
     * 按条件删除
     *
     * @param example 条件
     * @return int 成功条数
     */
    @Override
    public int deleteByExample(CollectInfoExample example) {
        return collectInfoMapper.deleteByExample(example);
    }

    /**
     * 按主键删除
     *
     * @param id
     * @return int 成功条数
     */
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return collectInfoMapper.deleteByPrimaryKey(id);
    }

    /**
     * 新增
     *
     * @param record 记录
     * @return int 成功条数
     */
    @Override
    public int insert(CollectInfo record) {
        record.setId(null);
        record.setUserdef1(String.valueOf(Integer.valueOf(record.getUserdef1())+1));
        ProMarketBase proMarketBase = proMarketBaseManager.selectByPrimaryKey(record.getMktId());
        return this.insert(record,proMarketBase);
    }

    /**
     * 新增
     *
     * @param collectInfo   记录
     * @param proMarketBase
     * @return int 成功条数
     */
    @Override
    public int insert(CollectInfo collectInfo, ProMarketBase proMarketBase) {
        BeanUtils.copyProperties(proMarketBase, collectInfo);
        collectInfo.setType(MarathonConst.COLLECTION_TYPE);
        collectInfo.setCreateEmp(MarathonConst.CREATE_EMP);
        collectInfo.setCreateTm(new Date());
        collectInfo.setCurNum(0);
        collectInfo.setStatus(CollectStatus.CREATED.getCode());
        collectInfo.setStartTm(new Date());
        collectInfo.setMktId(proMarketBase.getMktId());
        collectInfo.setNeedNum(proMarketBase.getGroupLimit().intValue());
        collectInfo.setMaxPkg(proMarketBase.getWeightMax().doubleValue());
        collectInfo.setEndTm(DateUtil.getEndTime(proMarketBase.getGroupDuration()));
        collectInfo.setNote(proMarketBase.getUseRequire());
        StringBuilder seq = new StringBuilder();
        for (int i = 0; i <  3-String.valueOf(collectInfo.getUserdef1()).length(); i++) {
            seq.append("0");
        }
        seq.append(String.valueOf(collectInfo.getUserdef1()));
        collectInfo.setName(proMarketBase.getMktNameShow()+ DateUtil.getDay()+seq);
        return collectInfoMapper.insert(collectInfo);
    }


    /**
     * 新增,null字段不插入
     *
     * @param record 记录
     * @return int 成功条数
     */
    @Override
    public int insertSelective(CollectInfo record) {
        return collectInfoMapper.insertSelective(record);
    }


    /**
     * 按条件查询
     *
     * @param example 条件
     * @return List<CollectInfo> 记录列表
     */
    @Override
    public List<CollectInfo> selectByExample(CollectInfoExample example) {
        return collectInfoMapper.selectByExample(example);
    }

    /**
     * 按主键查询
     *
     * @param id
     * @return CollectInfo 记录
     */
    @Override
    public CollectInfo selectByPrimaryKey(Integer id) {
        return collectInfoMapper.selectByPrimaryKey(id);
    }

    /**
     * 按条件更新,null字段不更新
     *
     * @param record  记录
     * @param example 条件
     * @return int 成功条数
     */
    @Override
    public int updateByExampleSelective(CollectInfo record, CollectInfoExample example) {
        return collectInfoMapper.updateByExampleSelective(record, example);
    }


    /**
     * 按条件更新
     *
     * @param record  记录
     * @param example 条件
     * @return int 成功条数
     */
    @Override
    public int updateByExample(CollectInfo record, CollectInfoExample example) {
        return collectInfoMapper.updateByExample(record, example);
    }

    @Override
    public int updateByIdAndStatus(CollectInfo record, Byte sourceState, Byte destState) {
        record.setStatus(destState);
        CollectInfoExample example = new CollectInfoExample();
        example.createCriteria().andStatusEqualTo(sourceState).andIdEqualTo(record.getId());
        return collectInfoMapper.updateByExample(record, example);

    }


    /**
     * 按主键更新,null的字段不更新
     *
     * @param record 记录
     * @return int 成功条数
     */
    public int updateByPrimaryKeySelective(CollectInfo record) {
        return collectInfoMapper.updateByPrimaryKeySelective(record);
    }


    /**
     * 按主键更新
     *
     * @param record 记录
     * @return int 成功条数
     */
    @Override
    public int updateByPrimaryKey(CollectInfo record) {
        return collectInfoMapper.updateByPrimaryKey(record);
    }

    @Override
    public CollectInfo queryUnFinishedCollectInfo(String type) {
        CollectInfoExample example = new CollectInfoExample();
        example.createCriteria().andStatusEqualTo(CollectStatus.CREATED.getCode()).andTypeEqualTo(MarathonConst.COLLECTION_TYPE);
        List<CollectInfo> collectInfos = collectInfoMapper.selectByExample(example);
        return CollectionUtils.isEmpty(collectInfos) ? null : collectInfos.get(0);

    }

	@Override
	public CollectInfo selectCollectInfo() {
		CollectInfoExample example = new CollectInfoExample();
		example.createCriteria().andStatusEqualTo((byte)CollectStatus.CREATED.getCode());
		List<CollectInfo> list = collectInfoMapper.selectByExample(example);
		CollectInfo info = list.get(0);
		int minMail = info.getMinMail().intValue();
		String note = info.getNote();
		note = note.replaceAll("@@" , String.valueOf(minMail));
		info.setNote(note);
		info.setEndTmLong(info.getEndTm().getTime());
		int cid = info.getId();
		CollectMemberExample collectMemberExample = new CollectMemberExample();
		collectMemberExample.createCriteria().andCidEqualTo(cid);
		List<CollectMember> members = collectMemberMapper.selectByExample(collectMemberExample);
		initMemberImg(info, members);
		
		
		return info;
	}

	private void initMemberImg(CollectInfo info, List<CollectMember> members) {
		if(CollectionUtils.isNotEmpty(members)){
			List<Integer> mIds = Lists.newArrayList();
			members.forEach(e->mIds.add(e.getId()));
			if(CollectionUtils.isNotEmpty(mIds)){
				RegInfoExample regInfoExample = new RegInfoExample();
				regInfoExample.createCriteria().andMidIn(mIds);
				List<RegInfo> regInfoList = refInfoMapper.selectByExample(regInfoExample);
				if(CollectionUtils.isNotEmpty(regInfoList)){
					List<String> imgs = Lists.newArrayList();
					regInfoList.forEach(e->imgs.add(e.getImgUrl()));
					info.setImgs(imgs);
				}
				
			}
			
			
		}
	}
	
	
    
}